package com.foxzzz.dynamic.programming;

public class Solution91 {

    public int numDecodings(String s) {
        if (s == null || s.equals("") || s.startsWith("0"))
            return 0;
        int num = 1;
        int pre = 1;

        for (int i = 1; i < s.length(); i++) {
            int sN=Integer.parseInt(s.substring(i - 1, i + 1)) ;
            if (s.charAt(i - 1) == '0') {
                if (s.charAt(i) == '0')
                    return 0;
            }else if (s.charAt(i) == '0'){
                pre = num;
                if(sN>26) {
                    return 0;
                }
            }
            else if (sN<=26) {
                if (i + 1 == s.length() || s.charAt(i + 1) != '0') {
                    num += pre;
                    pre = num - pre;
                }
            } else {

                pre = num;
            }
        }
        return num;
    }


    public static void main(String[] args) {
        System.out.println(new Solution91().numDecodings("10"));
        System.out.println(new Solution91().numDecodings("0"));
        System.out.println(new Solution91().numDecodings("12120"));
        System.out.println(new Solution91().numDecodings("230"));

    }
}
